package com.example.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.School;
import com.example.demo.mapper.SchoolMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/school")
public class SchoolController {


    @Resource
    SchoolMapper schoolMapper;
    //删除数据
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        schoolMapper.deleteById(id);
        return Result.success();
    }

    //添加数据
    @PostMapping
    public Result<?> save(@RequestBody School school) {
        schoolMapper.insert(school);
        return Result.success();
    }

    //更新数据
    @PutMapping
    public Result<?> update(@RequestBody School school) {
        schoolMapper.updateById(school);
        return Result.success();
    }

    //分页查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        Page<School> schoolPage = schoolMapper.selectPage(new Page<>(pageNum, pageSize),
                Wrappers.<School>lambdaQuery().like(School::getId, search));
        return Result.success(schoolPage);
    }






}
